<template>
  <div id="app">
    <!--使用需要的组建，可以引入多个-->
	  <top v-show="isShowTop"></top>
    <topsimple v-show="isShowTopSimple"></topsimple>
	  <topSimpleWithShare v-show="isShowTopSimpleWithShare"></topSimpleWithShare>
	  <topSimpleWithNew v-show="isShowTopSimpleWithNew"></topSimpleWithNew>
	  <footerBottom v-show="isShowTab"></footerBottom>
    <router-view @showTabbar="showTabbar" :msg="msg" @showTopbar="showTopbar" :topMsg="topMsg"></router-view>
  </div>
</template>

<script>
	import bus from '@/event/eventBus'
  import top from './components/common/top'
  import topsimple from './components/common/topsimple'
	import topSimpleWithShare from '../../components/mobile/common/topSimpleWithShare'
	import topSimpleWithNew from '../../components/mobile/common/topSimpleWithNew'
  import footerBottom from './components/common/footerBottom'
  import NetWorking from '../../utils/networking'
  export default {
    name: 'app',
    components: {
      top,
	    topsimple,
	    topSimpleWithShare,
	    topSimpleWithNew,
      footerBottom
    },
	  data() {
    	return {
		    msg: '',
    		isShowTab: true,
		    isShowTop: true,
		    isShowTopSimple: false,
		    isShowTopSimpleWithShare: false,
		    isShowTopSimpleWithNew: false,
		    topMsg: ''
	    }
	  },
	  created: function () {
		  let _this = this
		  bus.$on("barController", function (msg) {
			  if(msg == 0){
				  _this.isShowTab = true
				  _this.isShowTop = true
				  _this.isShowTopSimple = false
				  _this.isShowTopSimpleWithShare = false
				  _this.isShowTopSimpleWithNew = false
			  }else if(msg == 1){
				  _this.isShowTab = false
				  _this.isShowTop = false
				  _this.isShowTopSimple = true
				  _this.isShowTopSimpleWithShare = false
				  _this.isShowTopSimpleWithNew = false
			  }else if(msg == 2){
				  _this.isShowTab = false
				  _this.isShowTop = false
				  _this.isShowTopSimple = false
				  _this.isShowTopSimpleWithShare = true
				  _this.isShowTopSimpleWithNew = false
			  }else if(msg == 3){
				  _this.isShowTab = false
				  _this.isShowTop = false
				  _this.isShowTopSimple = false
				  _this.isShowTopSimpleWithShare = false
				  _this.isShowTopSimpleWithNew = true
			  }else if(msg == 9){
				  _this.isShowTab = false
				  _this.isShowTop = false
				  _this.isShowTopSimple = false
				  _this.isShowTopSimpleWithShare = false
				  _this.isShowTopSimpleWithNew = false
			  }

			  if(window.JPush != null){
				  window.JPush.getRegistrationID(this.onGetRegistrationID)
			  }
		  })
	  },
	  beforeCreate (){
		  this.$store.commit('FETCH_TOKEN')
		  this.$store.commit('FETCH_LAST_LOGIN_AS')
		  if(this.$store.state.user.token != null) {
			  NetWorking.doGet(
				  'auth/checkToken',
				  null,
				  null,
			  ).then(
				  response => {
					  let rs = response.data
					  if (rs.error_code == 0) {
						  this.initNotification()
						  window.setInterval(this.initNotification,30000);
					  } else {
						  location.replace('/login')
					  }
				  }
			  )
		  }else{
			  location.replace('/login')
		  }
	  },
	  methods: {
		  onGetRegistrationID(data) {
			  try {
				  alert("JPushPlugin:registrationID is " + data)

				  if (data.length == 0) {
					  var t1 = window.setTimeout(getRegistrationID, 1000)
				  }
				  this.regid = data
			  } catch (exception) {
				  console.log(exception)
			  }
		  },
		  showTabbar: function (msg) {
			  this.isShowTab = msg
		  },
		  showTopbar: function (topMsg) {
			  this.isShowTop = topMsg
		  },
    	initNotification(){
    		NetWorking.doGet(
    			'notification/getPendingActivityNum',
			    null,
			    {
			    	params:{
			    		type: 'S_LOV_LESSON',
					    rolePlay: 'teacher'
				    }
			    }
		    ).then(
		    	response => {
		    		let rs = response.data
				    if(rs.error_code == 0){
					    this.$store.dispatch('createPendingActNum', rs.pending_act_num)
				    }else{
		    			alert(rs.error_message)
				    }
			    }
		    )
	    }
	  }
  }
</script>

<style>
	body {
		background-color: #f4f6f7;
		margin: 0px;
	}
</style>
